package cst.a230ly.spinnertest230ly;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import static android.R.id.list;

public class MainActivity extends AppCompatActivity {
    private TextView tvinfo;
    private Spinner spinner;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();

        List<City> list=getDate();
        String[] cities=new String[list.size()];
        for(int i=0;i<list.size();i++)
            cities[i]=list.get(i).getName();

         //String[] cities={"北京","上海","广州","成都"};
        final ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item,cities);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
        spinner.setPrompt("");
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                tvinfo.setText("您选择的是：" + parent.getItemAtPosition(position).toString());
        }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });






    }

    private List<City> getDate() {
        List<City> list=new ArrayList<City>();
        City c;

        c=new City();
        c.setName("北京2");
        list.add(c);

        c=new City();
        c.setName("上海2");
        list.add(c);

        c=new City();
        c.setName("广州2");
        list.add(c);

        return list;
    }

    private void init(){
        tvinfo=(TextView)findViewById(R.id.tvinfo);
        spinner=(Spinner)findViewById(R.id.spinner);
    }



}
